import { publicRoutes, asyncRoutes } from '@/router';

export default {
  namespaced: true,
  state: () => ({
    routes: publicRoutes
  }),
  mutations: {
    setRoutes(state, newRoutes) {
      state.routes = [...publicRoutes, ...newRoutes]; // 合并公共路由和动态路由
    }
  },
  actions: {
    filterRoutes(context, menus) {
      // 筛选后需要展示的路由列表
      const routes = [];

      menus.forEach((key) => {
        routes.push(...asyncRoutes.filter((item) => item.name === key));
      });

      // 所有不匹配的路由，进入404
      // 该路由必须在其他所有路由指定以后
      routes.push({
        path: '/:catchAll(.*)',
        redirect: '/404'
      });

      context.commit('setRoutes', routes);

      return routes;
    }
  }
};
